
#include "CGUIObjManager.h"
#include "CGUIControl.h"
#include "CGUIWinControl.h"
#include "CGUIButton.h"
#include "CGUIFileLoader.h"
#include "CGUIGui.h"
#include "CStatuManager.h"
#include "CGUIImage.h"
#include "CGUIScrollBar.h"
#include "CGUILabel.h"
CGUIObjManager::CGUIObjManager(void)
{

	m_pCurCtrl=NULL;
}

CGUIObjManager::~CGUIObjManager(void)
{
}


CGUIObjManager& CGUIObjManager::Instance(void)
{
	static CGUIObjManager instance;
	return instance;
}
void CGUIObjManager::CreateForm(const TiXmlElement* element)
{
	WCHAR szPosition[MAX_PATH];
	WCHAR szName[MAX_PATH];
	WCHAR szBmp[MAX_PATH];
	
	CharToWide(szName,element->Attribute("name"));
	CharToWide(szPosition,element->Attribute("position"));	
	CharToWide(szBmp,element->Attribute("bitmap"));

	RECT rc;
	StrToRect(szPosition,rc);
    CGUIWinControl *pCtrl=STATUSMGR.GetStatu(szName);
	pCtrl->Create(NULL, rc.left, rc.top,rc.right-rc.left ,rc.bottom-rc.top, RGB16(0, 0, 0));
	CGUIFileLoader Loader;
	Loader.SetDirApp(_T("Pic"));
	m_background.LoadBMP(Loader.Load(szBmp));
	 InsertGUIControlMap(szName,pCtrl);
  	m_pCurCtrl=pCtrl;

}
void CGUIObjManager::CreatePanel(const TiXmlElement* element)
{

	WCHAR szPosition[MAX_PATH]={0};
	WCHAR szName[MAX_PATH]={0};
	WCHAR szTarget[MAX_PATH]={0};
	WCHAR szBmp[MAX_PATH]={0};
	WCHAR szCaption[MAX_PATH]={0};

	CharToWide(szName,element->Attribute("name"));
	CharToWide(szPosition,element->Attribute("position"));	
	CharToWide(szBmp,element->Attribute("bitmap"));


	RECT rc;
	StrToRect(szPosition,rc);

	CGUIPanel *pCtrl=new CGUIPanel;

	pCtrl->Create(m_CtrlStack.top(), rc.left, rc.top, szBmp);
	m_pCurCtrl=pCtrl;
	 InsertGUIControlMap(szName,pCtrl);
	

}

void CGUIObjManager::InsertGUIControlMap(wstring name,CGUIControl * p)
{
   m_mapElement.insert(name.c_str(),p);
}
CGUIControl* CGUIObjManager::GetGUIControl(const WCHAR* elementName)
{
	CGUIControl* pRet = (CGUIControl*)m_mapElement.getValue(elementName);
	return pRet;
}
void CGUIObjManager::CreateButton(const TiXmlElement* element)
{



	WCHAR szPosition[MAX_PATH]={0};
	WCHAR szName[MAX_PATH]={0};
	WCHAR szTarget[MAX_PATH]={0};
	WCHAR szBmp[MAX_PATH]={0};
    WCHAR szCaption[MAX_PATH]={0};
	WCHAR szTextPos[MAX_PATH]={0};
    WCHAR szAlina[MAX_PATH]={0};
	WCHAR szFont[MAX_PATH]={0};
	CharToWide(szName,element->Attribute("name"));
	CharToWide(szPosition,element->Attribute("position"));	
	CharToWide(szBmp,element->Attribute("bitmap"));
	CharToWide(szTarget,element->Attribute("target"));
	CharToWide(szCaption,element->Attribute("text"));
	CharToWide(szTextPos,element->Attribute("textpos"));
	CharToWide(szFont,element->Attribute("font"));

	RECT rc;
	StrToRect(szPosition,rc);
    POINT pt;
	StrToPoint(szTextPos,pt);
	CGUIButton *pCtrl=new CGUIButton;
   
    pCtrl->Create(m_CtrlStack.top(), rc.left, rc.top, szBmp, szCaption,pt.x,pt.y,_wtoi(szAlina));
    pCtrl->SetFont(szFont);
	pCtrl->SetDownOffset(-1, 3);

	m_pCurCtrl=pCtrl;

    InsertGUIControlMap(szName,pCtrl);


}



void CGUIObjManager::CreateImage(const TiXmlElement* element)
{



	WCHAR szPosition[MAX_PATH]={0};
	WCHAR szName[MAX_PATH]={0};
	WCHAR szTarget[MAX_PATH]={0};
	WCHAR szBmp[MAX_PATH]={0};
	WCHAR szCaption[MAX_PATH]={0};

	CharToWide(szName,element->Attribute("name"));
	CharToWide(szPosition,element->Attribute("position"));	
	CharToWide(szBmp,element->Attribute("bitmap"));


	RECT rc;
	StrToRect(szPosition,rc);

	CGUIImage *pCtrl=new CGUIImage;

	pCtrl->Create(m_CtrlStack.top(), rc.left, rc.top, szBmp);


	m_pCurCtrl=pCtrl;

	InsertGUIControlMap(szName,pCtrl);


}
void CGUIObjManager::CreateSlider(const TiXmlElement* element)
{
	WCHAR szPosition[MAX_PATH]={0};
	WCHAR szName[MAX_PATH]={0};
	WCHAR szBmp[MAX_PATH]={0};
	WCHAR szBmpBG[MAX_PATH]={0};
	WCHAR szBmpFG[MAX_PATH]={0};

	CharToWide(szName,element->Attribute("name"));
	CharToWide(szPosition,element->Attribute("position"));	
	CharToWide(szBmp,element->Attribute("bitmap"));
	CharToWide(szBmpBG,element->Attribute("bitmapbg"));
	CharToWide(szBmpFG,element->Attribute("bitmapfg"));


	RECT rc;
	StrToRect(szPosition,rc);

	CGUIScrollBar *pCtrl=new CGUIScrollBar;

	pCtrl->Create(m_CtrlStack.top(), rc.left, rc.top,(ScrollBarKind)0 ,szBmp,szBmpBG,szBmpFG,rc);


	m_pCurCtrl=pCtrl;

	InsertGUIControlMap(szName,pCtrl);
}

void CGUIObjManager::CreateLable(const TiXmlElement* element)
{
	WCHAR szPosition[MAX_PATH]={0};
	WCHAR szName[MAX_PATH]={0};
	WCHAR szCaption[MAX_PATH]={0};
	WCHAR szAlina[MAX_PATH]={0};
    WCHAR szFont[MAX_PATH]={0};

	CharToWide(szName,element->Attribute("name"));
	CharToWide(szPosition,element->Attribute("position"));	
	CharToWide(szFont,element->Attribute("font"));
	CharToWide(szAlina,element->Attribute("alina"));
    CharToWide(szCaption,element->Attribute("text"));

	RECT rc;
	StrToRect(szPosition,rc);

	CGUILabel *pCtrl=new CGUILabel;

	pCtrl->Create(m_CtrlStack.top(), rc.left, rc.top,rc.right,rc.bottom,szFont,_wtoi(szAlina),szCaption);


	m_pCurCtrl=pCtrl;

	InsertGUIControlMap(szName,pCtrl);

}
void CGUIObjManager::Parse(TiXmlNode *this_node,  TCHAR *filename)
{
	TiXmlElement* element = NULL;
	TiXmlNode *node = NULL;
	const char *value = NULL;
	TCHAR  szFilePath[MAX_PATH]={0};

	TiXmlDocument doc;
	bool bExternalLoad = false;
 
	
 
	if(filename[0] != 0)
	{
		wcscpy(szFilePath, GetAppPath());
		wcscat(szFilePath,filename);
		FILE * hF = _wfopen(szFilePath,L"rb");
		if(!doc.LoadFile(hF))	// Try to parse data using given filename
		{

			return ;
		}
        fclose(hF);
		node = doc.FirstChild();	// Aquire first data node
		bExternalLoad = true;
	}



	if(bExternalLoad)
	{
		element = node->ToElement();

		
		if(value!= NULL)
		{
	
		}

	

		Parse(node, L"");

	}
	else
	{
		
		element = this_node->ToElement();

		value = element->Attribute("position");
	

		TiXmlNode *child_node = NULL;
		child_node = this_node->FirstChild();

		while(child_node !=NULL)
		{
			if(strcmp(child_node->Value(), "Frame") == 0)	// We got a Frame element	
				CreateForm(child_node->ToElement());
			else if (strcmp(child_node->Value(), "Button") == 0)
				CreateButton(child_node->ToElement());
			else if (strcmp(child_node->Value(), "Panel")== 0)
				CreatePanel(child_node->ToElement());
			else if(strcmp(child_node->Value(), "Image")== 0)
				CreateImage(child_node->ToElement());
			else if(strcmp(child_node->Value(), "Slider")== 0)
				CreateSlider(child_node->ToElement());
            else if(strcmp(child_node->Value(), "Text")== 0)
				CreateLable(child_node->ToElement());
			
			
            m_CtrlStack.push(m_pCurCtrl);
			Parse(child_node, L"");
			m_CtrlStack.pop();

		
			child_node = this_node->IterateChildren(child_node);
		}
	}

}
void CGUIObjManager::RemoveAll()
{

  m_mapElement.clear();
}